package com.ruoyi.card.controller;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.IdUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.card.domain.CardSecret;
import com.ruoyi.card.service.ICardSecretService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 卡密管理Controller
 * 
 * @author shan
 * @date 2022-06-08
 */
@Controller
@RequestMapping("/cardSecret/secret")
public class CardSecretController extends BaseController
{
    private String prefix = "cardSecret/secret";

    @Autowired
    private ICardSecretService cardSecretService;

    @RequiresPermissions("cardSecret:secret:view")
    @GetMapping()
    public String secret()
    {
        return prefix + "/secret";
    }

    /**
     * 查询卡密管理列表
     */
    @RequiresPermissions("cardSecret:secret:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CardSecret cardSecret)
    {
        startPage();
        List<CardSecret> list = cardSecretService.selectCardSecretList(cardSecret);
        return getDataTable(list);
    }

    /**
     * 导出卡密管理列表
     */
    @RequiresPermissions("cardSecret:secret:export")
    @Log(title = "卡密管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CardSecret cardSecret)
    {
        List<CardSecret> list = cardSecretService.selectCardSecretList(cardSecret);
        ExcelUtil<CardSecret> util = new ExcelUtil<CardSecret>(CardSecret.class);
        return util.exportExcel(list, "卡密管理数据");
    }


    /**
     * 新增卡密管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        CardSecret cardSecret=new CardSecret();
        cardSecret.setCardId(IdUtil.randomUUID());
        cardSecret.setCardState(1);
        cardSecret.setCardType(1);
        LocalDateTime localDateTime= LocalDateTime.now().plusDays(31);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        Date date = Date.from(zdt.toInstant());
        cardSecret.setExpireDatetime(date);
        mmap.put("cardSecret", cardSecret);

        return prefix + "/add";
    }

    /**
     * 新增保存卡密管理
     */
    @RequiresPermissions("cardSecret:secret:add")
    @Log(title = "卡密管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CardSecret cardSecret)
    {
        return toAjax(cardSecretService.insertCardSecret(cardSecret));
    }

    /**
     * 修改卡密管理
     */
    @RequiresPermissions("cardSecret:secret:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CardSecret cardSecret = cardSecretService.selectCardSecretById(id);
        mmap.put("cardSecret", cardSecret);
        return prefix + "/edit";
    }

    /**
     * 修改保存卡密管理
     */
    @RequiresPermissions("cardSecret:secret:edit")
    @Log(title = "卡密管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CardSecret cardSecret)
    {
        return toAjax(cardSecretService.updateCardSecret(cardSecret));
    }

    /**
     * 删除卡密管理
     */
    @RequiresPermissions("cardSecret:secret:remove")
    @Log(title = "卡密管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cardSecretService.deleteCardSecretByIds(ids));
    }
}
